﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using EntityObject;
using SharedLibraries;
using SharedLibraries.Constants;
using ThangNM_LIB.FUNC;
using ThangNM_LIB.EO;

namespace ThangNM.UserControl
{
    public partial class ArticleUC : System.Web.UI.UserControl
    {
        #region "Properties & Event"
        public String sLinkImage
        {
            get { return (String)ViewState["sLinkImage"]; }
            set { ViewState["sLinkImage"] = value; }
        }
        public String sLinkVideo
        {
            get { return (String)ViewState["sLinkVideo"]; }
            set { ViewState["sLinkVideo"] = value; }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void BindData_Topic(TopicEO _TopicEO)
        {
            mtvMain.SetActiveView(vTopic);
            lblTopic_Description.Text = _TopicEO.sDescription;
            imgTopic_LinkImage.ImageUrl = _TopicEO.sLinkImage;
            hplTopic_LinkImage.NavigateUrl = _TopicEO.sLinkImage;
            imgTopic_LinkImage.AlternateText = _TopicEO.sTitle;
            lblTopic_Content.Text = _TopicEO.sContent;
            Commons.setBreadcrumb(_TopicEO, lblBreadcrumb);
        }

        public void BindData_Author(AccountEO _AccountEO)
        {
            mtvMain.SetActiveView(vAuthor);

            //hplLevel3.Text = _AccountEO.sUsername;
            hplsLinkAvatar.NavigateUrl = _AccountEO.sLinkAvatar;
            imgsLinkAvatar.ImageUrl = _AccountEO.sLinkAvatar;
            imgsLinkAvatar.AlternateText = _AccountEO.sFullName;
            lblsFullName.Text = _AccountEO.sFullName;
            lblsAddress.Text = _AccountEO.sAddress;
            lblsPhoneNumber.Text = _AccountEO.sPhoneNumber;
            lbliPermission.Text = GetListConstants.getValueKeyInSortedList(GetListConstants.Account_iPermission_GLC(), _AccountEO.iPermission, false);
            Commons.setBreadcrumb(_AccountEO, lblBreadcrumb);
        }

        public void BindData_File(FileEO _FileEO)
        {
            mtvMain.SetActiveView(vFile);
            //lbliQuantityDownload.Text = Convert.ToString(_FileEO.iQuantityDownload);
            Int64 iSize = 0;
            Int64.TryParse(_FileEO.sSize, out iSize);
            lbliSize.Text = ConvertEx.BytesToString(iSize);
            lblsDescription.Text = Convert.ToString(_FileEO.sDescription);
            //lblsName.Text = Convert.ToString(_FileEO.sName);
            //lblsName1.Text = Convert.ToString(_FileEO.sName);
            lblsKeyword.Text = Convert.ToString(_FileEO.sKeyword);

            if (String.IsNullOrEmpty(StringEx.Trim(_FileEO.sEmbed)))
                lbtnPreview.Visible = false;
            else
                lbtnPreview.Visible = true;

            List<sLinkEO> lstLink = new List<sLinkEO>();
            Object temp = JsonEx.Json2ListObject(_FileEO.sLinkDownload, lstLink.GetType());
            if (temp != null)
                lstLink = (List<sLinkEO>)temp;
            String sUrlDownload = "";
            if (lstLink.Count > 0)
            {
                foreach (var item in lstLink)
                {
                    if (_FileEO.bAdvertise && !String.IsNullOrEmpty(item.sLinkAds))
                        item.sLink = item.sLinkAds;
                    item.sLinkAds = ">> link " + GetTextConstants.File_iServer_GTC(item.iServer);
                }
                lstLink = (from i in lstLink where i.bStatus == true select i).ToList();
                if (lstLink.Count > 0)
                    if (!String.IsNullOrEmpty(lstLink[0].sLink))
                        sUrlDownload = lstLink[0].sLink;

                rptDownload.DataSource = lstLink;
                rptDownload.DataBind();
            }
            FSE.SOFT.QRCode.Codec.QRCodeEncoder qrCodeEncoder = new FSE.SOFT.QRCode.Codec.QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = FSE.SOFT.QRCode.Codec.QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeVersion = 5;
            qrCodeEncoder.QRCodeScale = 5;
            qrCodeEncoder.QRCodeErrorCorrect = 0;
            var bmpImage = qrCodeEncoder.Encode(sUrlDownload);
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            bmpImage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] imageBytes = stream.ToArray();
            imgQRCode.ImageUrl = @"data:image/gif;base64," + Convert.ToBase64String(imageBytes);
            imgQRCode.AlternateText = _FileEO.sTitle;
            hplQRCode.NavigateUrl = sUrlDownload;

            Commons.setBreadcrumb(_FileEO, lblBreadcrumb);
        }

        public void BindData_Media(MediaEO _MediaEO)
        {
            mtvMain.SetActiveView(vMedia);

            sLinkImage = _MediaEO.sLinkImage;
            imgGif.ImageUrl = _MediaEO.sLinkMedia;
            imgGif.AlternateText = _MediaEO.sTitle;
            hplGif.NavigateUrl = _MediaEO.sLinkMedia;
            //imgFullScreen.ImageUrl = _MediaEO.sLinkMedia;
            sLinkVideo = _MediaEO.sLinkMedia;
            switch (GetTextConstants.ExtendFile_GTC(System.IO.Path.GetExtension(_MediaEO.sLinkMedia)))
            {
                case ExtendFile_C.SWF: pnlFlash.Visible = true; break;
                    //pnlFlash_Full.Visible = true; 
                case ExtendFile_C.GIF: pnlImage.Visible = true; break;
                    //pnlImage_Full.Visible = true;
                case ExtendFile_C.FLV: pnlVideo.Visible = true; mtvVideo.ActiveViewIndex = 0; break;
                case ExtendFile_C.MP4: pnlVideo.Visible = true; mtvVideo.ActiveViewIndex = 0; break;
                case ExtendFile_C.OGG: pnlVideo.Visible = true; mtvVideo.ActiveViewIndex = 1; break;
                case ExtendFile_C.WEBM: pnlVideo.Visible = true; mtvVideo.ActiveViewIndex = 1; break;
                case ExtendFile_C.F4V: pnlVideo.Visible = true; mtvVideo.ActiveViewIndex = 1; break;
                case ExtendFile_C.MKV: pnlVideo.Visible = true; mtvVideo.ActiveViewIndex = 1; break;
                case ExtendFile_C.AVI: pnlVideo.Visible = true; mtvVideo.ActiveViewIndex = 2; break;
                case ExtendFile_C._3G2: pnlVideo.Visible = true; mtvVideo.ActiveViewIndex = 2; break;
                case ExtendFile_C.MOV: pnlVideo.Visible = true; mtvVideo.ActiveViewIndex = 2; break;
                case ExtendFile_C.MPG: pnlVideo.Visible = true; mtvVideo.ActiveViewIndex = 2; break;
                case ExtendFile_C.WMV: pnlVideo.Visible = true; mtvVideo.ActiveViewIndex = 2; break;
                default: mtvVideo.ActiveViewIndex = 3; break;
            }
            lblFK_iAccountID.Text = _MediaEO.FK_sAccount;
            hplFK_iAccountID.NavigateUrl = OtherEx.AddDomainInUrl(FileNameByFunction.Tac_Gia_ + _MediaEO.FK_sAccount + FileNameByFunction.HTML);
            lbliVisit.Text = _MediaEO.iVisit.ToString();
            lbliLike.Text = _MediaEO.iLike.ToString();
            lbltDateTimeCreate.Text = _MediaEO.tDateTimeCreate.ToString(FormatEx.Date_Time.Format_DateTime_WebView);

            //hplTag.Text = Convert.ToString(_Category_ZipEO.sName);
            //hplTag.ToolTip = Convert.ToString(_Category_ZipEO.sName);
            //hplTag.NavigateUrl = OtherEx.AddDomainInUrl(_Category_ZipEO.sUrlRewrite);
            Commons.setBreadcrumb(_MediaEO, lblBreadcrumb);
        }

        public void BindData_Tool(MediaEO _MediaEO)
        {
            mtvMain.SetActiveView(vTool);
            Commons.setBreadcrumb(_MediaEO, lblBreadcrumb);
        }

        public void BindData_Common(DataTable dtAtt, DataTable dtOther, SEOEO _SEOEO, Boolean isShowCmt)
        {
            lblsTitle.Text = _SEOEO.sTitle;
            lblsTitle1.Text = _SEOEO.sTitle;
            lblFK_iAccountID.Text = _SEOEO.FK_sAccount;
            hplFK_iAccountID.NavigateUrl = OtherEx.AddDomainInUrl(FileNameByFunction.Tac_Gia_ + _SEOEO.FK_sAccount + FileNameByFunction.HTML);
            lbliComment.Text = Convert.ToString(_SEOEO.iComment);
            lbliVisit.Text = Convert.ToString(_SEOEO.iVisit);
            lbliLike.Text = Convert.ToString(_SEOEO.iLike);
            lbltDateTimeCreate.Text = _SEOEO.tDateTimeCreate.ToString(FormatEx.Date_Time.Format_DateTime_WebView);
            Commons.setTags(rptTag, _SEOEO.FK_sCategory);

            if (cphControl != null)
            {
                cphControl.Controls.Clear();
                if (dtAtt != null)
                {
                    var ctlAtt = (ThangNM.UserControl.AttachFileUC)this.Page.LoadControl("~/UserControl/AttachFileUC.ascx");
                    ctlAtt.BindData(dtAtt);
                    cphControl.Controls.Add(ctlAtt);
                }

                var ctlCmt = (ThangNM.UserControl.Vote_Share_CommentUC)this.Page.LoadControl("~/UserControl/Vote_Share_CommentUC.ascx");
                ctlCmt.BindData_NEW(_SEOEO, isShowCmt);
                cphControl.Controls.Add(ctlCmt);

                if (dtOther != null)
                {
                    var ctlOther = (ThangNM.UserControl.OtherUC)this.Page.LoadControl("~/UserControl/OtherUC.ascx");
                    ctlOther.BindData(dtOther);
                    cphControl.Controls.Add(ctlOther);
                }
            }
        }

    }
}